#define  _CRT_SECURE_NO_WARNINGS
class Solution {
public:
    vector<vector<int>> matrixBlockSum(vector<vector<int>>& mat, int k) {
        int m = mat.size(), n = mat[0].size();
        vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
        for (int i = 1; i <= m; i++)
            for (int j = 1; j <= n; j++)
                dp[i][j] = mat[i - 1][j - 1] + dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1];
        vector<vector<int>> ans(m, vector<int>(n));
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
            {
                int x1 = max(i - k, 0) + 1,
                    y1 = max(j - k, 0) + 1,
                    x2 = min(i + k, m - 1) + 1,
                    y2 = min(j + k, n - 1) + 1;
                ans[i][j] = dp[x2][y2]
                    - dp[x2][y1 - 1]
                    - dp[x1 - 1][y2]
                    + dp[x1 - 1][y1 - 1];
            }
        return ans;
    }
};